﻿// -----------------------------------------------------------------------
// <copyright file="TimeSpanToAngleConverter.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Noc.Pomodoro.Converters
{
    using System;
    using System.Globalization;

    /// <summary>
    /// One-way value converter to transform date times into 360-degree angle measurements.
    /// </summary>
    public class TimeSpanToAngleConverter : OneWayValueConverter
    {
        /// <summary>
        /// Converts a time span to an angle measurement for an analog clock.
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">This parameter is not used.</param>
        /// <param name="parameter">Optional. A <c>AnalogTimePart</c>, indicating which timespan component to convert. Default: <c>Hour</c>.</param>
        /// <param name="culture">This parameter is not used.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is TimeSpan)
            {
                var timespan = (TimeSpan)value;
                var timepart = (AnalogTimePart)(parameter ?? AnalogTimePart.Hour);
                switch (timepart)
                {
                    case AnalogTimePart.Hour:
                        return (timespan.Hours * 30) + (timespan.Minutes / 2) - 90;
                    case AnalogTimePart.Minute:
                        return (timespan.Minutes * 6) - 90;
                    case AnalogTimePart.Second:
                        return (timespan.Seconds * 6) - 90;
                    default:
                        return -90;
                }
            }
            else
            {
                throw new ArgumentException("Value must be of type System.TimeSpan.", "value");
            }
        }
    }
}